<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/12/21
 * Time: 16:52
 */

namespace app\admin\validator;

use lib\common\CommonValidator;

class SettingItemValidator extends CommonValidator
{
    protected $rule = [
        'name'    => 'require|max:255',
        'key'     => 'require|max:255',
        'type'    => 'require',
        'options' => 'require|validateOptions',
    ];

    protected $message = [
        'name'    => '配置项名称必填, 不能超过255字',
        'key'     => '配置项标识必填, 不能超过255字',
        'type'    => '请选择表单类型'
    ];

    protected $scene = [];

    protected function validateOptions ($value, $rule, $data): bool|string {
        if (in_array($data['type'], ['radio', 'checkbox', 'select', 'select:multiple'])) {
            if (empty($value) || !is_array($value)) {
                return '请填写备选项';
            }
            foreach ($value as $item) {
                if (empty($item['label']) || !isset($item['value'])) {
                    return '请填写备选项';
                }
            }
        }
        return true;
    }
}
